package com.xhd.serverComm;

import java.util.Hashtable;
import java.util.Map;
import java.util.Vector;

import com.xhd.base.log.Log;
import com.xhd.base.log.LogFactory;
import com.xhd.base.session.Session;
import com.xhd.base.session.SessionManager;
import com.xhd.server.ServerConfig;
import com.xhd.util.XThread;

public class DataProcessThread extends XThread
{
  private Hashtable jm570 = new Hashtable();
  private Hashtable jm571 = new Hashtable();
  private SessionManager jm213 = null;
  private static final Log jm9;
  static Class jm10;

  static
  {
    Class tmp3_0 = jm10;
    if (tmp3_0 == null)
    {
     
      try
      {
         DataProcessThread.jm10 = Class.forName("com.xhd.servercomm.DataProcessThread");
      }
      catch (ClassNotFoundException localClassNotFoundException)
      {
        throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
      }
    }
    jm9 = LogFactory.getLog(tmp3_0);
  }

  public DataProcessThread()
  {
    super(ServerConfig.SystemName + " Data Process Thread");
  }

  private void jm567(DataPack paramDataPack)
  {
    int i = 0;
    Object localObject;
    String str;
    if (paramDataPack.DataType.equals("<login>"))
    {
      i = 1;
      try
      {
        this.jm213.addSession(new Session((Map)paramDataPack.Data.firstElement()));
      }
      catch (Exception localException)
      {
        jm9.error(localException);
      }
    }
    else if (paramDataPack.DataType.equals("<logout>"))
    {
      i = 1;
      localObject = (String)paramDataPack.Data.firstElement();
      this.jm213.removeSession((String)localObject);
    }
    else if (paramDataPack.DataType.equals("<newemailcount>"))
    {
      i = 1;
      if ((paramDataPack.Data != null) && (paramDataPack.Data.size() > 0))
      {
        localObject = (String)paramDataPack.Data.get(0);
        str = (String)paramDataPack.Data.get(1);
     //   NewEmailPool.addNewEmailCount((String)localObject, str); //xhd   need  
      }
    }
    else if (paramDataPack.DataType.equals("<actiontime>"))
    {
      i = 1;
      this.jm213.flashActionTime(paramDataPack.Data);
    }
    if (i != 0)
      return;
    if ("<refreshdatapool>".equals(paramDataPack.DataType))
    {
      if ((ServerConfig.dataPool == null) || (paramDataPack.Data == null) || (paramDataPack.Data.size() <= 0))
        return;
      ServerConfig.dataPool.onRefresh(paramDataPack.Data);
    }
    else if ("<online>".equals(paramDataPack.DataType))
    {
      if ((paramDataPack.Data == null) || (1 != paramDataPack.Data.size()))
        return;
      OnlineMgr.getInstance().setOnlines((Map)paramDataPack.Data.get(0));
    }
    else if ("<refreshsession>".equals(paramDataPack.DataType))
    {
      this.jm213.refreshSessionsById(paramDataPack.Data);
    }
    else
    {
      localObject = jm569(paramDataPack.DataType);
      if (localObject != null)
      {
        str = paramDataPack.DataType.substring(1, paramDataPack.DataType.length() - 1);
        ((CommEventObject)localObject).onReceiveData(str, paramDataPack.Data);
      }
      else
      {
        jm9.info("DataProcess: No data in " + paramDataPack.DataType);
      }
    }
  }

  private void jm568(DataPack paramDataPack)
  {
    if (paramDataPack.DataType == null)
      return;
    Vector localVector1 = new Vector();
    Object localObject1;
    Object localObject2;
    if (paramDataPack.DataType.equals("<actiontime>"))
    {
      localObject1 = paramDataPack.Data;
      if ((localObject1 != null) && (1 == ((Vector)localObject1).size()) && (ServerConfig.ServerType == 1))
        ServerConfig.OnlineCount = Long.parseLong((String)paramDataPack.Data.get(0));
      localObject2 = this.jm213.getLoginUsers();
      long l1 = System.currentTimeMillis();
      if ((localObject2 != null) && (((Vector)localObject2).size() > 0))
        for (int i = 0; i < ((Vector)localObject2).size(); ++i)
        {
          Session localSession = (Session)((Vector)localObject2).get(i);
          Vector localVector2 = new Vector();
          localVector2.add(localSession.getId());
          long l2 = localSession.getActionTime();
          if (l1 - l2 >= 60000 * ServerConfig.OnlineTimeOutMinutes)
            continue;
          localVector2.add(new Long(l2));
          localVector1.add(localVector2);
        }
      if (localVector1.size() < 1)
        localVector1 = null;
    }
    else if ("<refreshdatapool>".equals(paramDataPack.DataType))
    {
      if ((ServerConfig.dataPool != null) && (paramDataPack.Data != null) && (paramDataPack.Data.size() > 0))
        ServerConfig.dataPool.onRefresh(paramDataPack.Data);
    }
    else if ("<refreshsession>".equals(paramDataPack.DataType))
    {
      this.jm213.refreshSessionsById(paramDataPack.Data);
    }
    else
    {
      localObject1 = jm569(paramDataPack.DataType);
      if (localObject1 != null)
      {
        localObject2 = paramDataPack.DataType.substring(1, paramDataPack.DataType.length() - 1);
        localVector1 = ((CommEventObject)localObject1).onReceiveReq((String)localObject2, paramDataPack.Data);
      }
      else
      {
        jm9.info("DataProcessThread no request in " + paramDataPack.DataType);
      }
    }
    if (localVector1 == null)
      return;
    DataTranCenter.addSendData(paramDataPack.SourceServer, paramDataPack.DataType, localVector1);
  }

  public void run()
  {
    Enumeration localEnumeration = ServerConfig.ServerCommEvent.elements();
    Object localObject;
    while ((localEnumeration != null) && (localEnumeration.hasMoreElements()))
    {
      localObject = (Hashtable)localEnumeration.nextElement();
      String str1 = (String)((Hashtable)localObject).get("event-type");
      String str2 = (String)((Hashtable)localObject).get("event-class");
      this.jm570.put("<" + str1 + ">", str2);
    }
    this.stop = false;
    while (!(this.stop))
    {
      localObject = DataTranCenter.getReceiveData();
      label170: if (localObject != null)
        try
        {
          if ("<flag_req>".equals(((DataPack)localObject).Type))
          {
            jm568((DataPack)localObject);
            break label170:
          }
          if ("<flag_data>".equals(((DataPack)localObject).Type))
            jm567((DataPack)localObject);
        }
        catch (Exception localException)
        {
          jm9.error(localException);
        }
      else
        try
        {
          sleep(1000L);
        }
        catch (InterruptedException localInterruptedException)
        {
        }
    }
  }

  private CommEventObject jm569(String paramString)
  {
    CommEventObject localCommEventObject = null;
    Object localObject = this.jm570.get(paramString);
    if (localObject != null)
    {
      localCommEventObject = (CommEventObject)this.jm571.get(localObject);
      if (localCommEventObject == null)
      {
        String str = (String)localObject;
        if (str.indexOf(46) < 0)
          str = "com.sinocc.servercomm." + str;
        try
        {
          localCommEventObject = (CommEventObject)Class.forName(str).newInstance();
          this.jm571.put(localObject, localCommEventObject);
        }
        catch (Throwable localThrowable)
        {
          jm9.error(localThrowable);
        }
      }
    }
    return localCommEventObject;
  }
}
